package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCrmKhggc;
import com.ruoyi.system.service.ISysCrmKhggcService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户公共池Controller
 * 
 * @author SYF
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/system/crmkhggc")
public class SysCrmKhggcController extends BaseController
{
    @Autowired
    private ISysCrmKhggcService sysCrmKhggcService;

    /**
     * 查询客户公共池列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysCrmKhggc sysCrmKhggc) {
        SysCrmKhggc param = new SysCrmKhggc();
        if(sysCrmKhggc.getKhProvince()!=null){
            if(!sysCrmKhggc.getKhProvince().equalsIgnoreCase("all")){
                param.setKhProvince(sysCrmKhggc.getKhProvince());
            }
        }
        if(sysCrmKhggc.getKhCity()!=null){
            if(!sysCrmKhggc.getKhCity().equalsIgnoreCase("all")){
                param.setKhCity(sysCrmKhggc.getKhCity());
            }
        }
        if(sysCrmKhggc.getKhFrom()!=null){
            if(!sysCrmKhggc.getKhFrom().equalsIgnoreCase("all")){
                param.setKhFrom(sysCrmKhggc.getKhFrom());
            }
        }
        if(sysCrmKhggc.getKhName()!=null&&!"".equals(sysCrmKhggc.getKhName())){
            param.setKhName(sysCrmKhggc.getKhName().replaceAll(" ",""));
        }
        if(sysCrmKhggc.getKhHandset()!=null&&!"".equals(sysCrmKhggc.getKhHandset())){
            param.setKhHandset(sysCrmKhggc.getKhHandset().replaceAll(" ",""));
        }
        startPage();
        List<SysCrmKhggc> list = sysCrmKhggcService.selectSysCrmKhggcList(param);
        return getDataTable(list);
    }

    @GetMapping("/getCities")
    public AjaxResult getCities(SysCrmKhggc sysCrmKhggc) {
        List<Map<String,Object>> cities = new ArrayList<>();
        Map<String,Object> cityMap = new HashMap<>();
        cityMap.put("value","all");
        cityMap.put("label","(全部)");
        cities.add(cityMap);
        List<Map<String,Object>> citiesList = sysCrmKhggcService.selectCities(sysCrmKhggc.getKhProvince());
        if(citiesList!=null&&citiesList.size()>0){
            for(Map<String,Object> city:citiesList){
                cities.add(city);
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("cities",cities);
        return AjaxResult.success(res);
    }
    @GetMapping("/getInitParams")
    public AjaxResult getInitParams() {
        List<Map<String,Object>> countries = new ArrayList<>();
        Map<String,Object> countriesMap = new HashMap<>();
        countriesMap.put("value","中国");
        countriesMap.put("label","中国");
        countries.add(countriesMap);
        List<Map<String,Object>> provinces = new ArrayList<>();
        Map<String,Object> provinceMap = new HashMap<>();
        provinceMap.put("value","all");
        provinceMap.put("label","(全部)");
        provinces.add(provinceMap);
        List<Map<String,Object>> sources = new ArrayList<>();
        Map<String,Object> sourceMap = new HashMap<>();
        sourceMap.put("value","all");
        sourceMap.put("label","(全部)");
        sources.add(sourceMap);
        List<Map<String,Object>> provincesList = sysCrmKhggcService.selectProvinces();
        List<Map<String,Object>> sourcesList = sysCrmKhggcService.selectSources();
        if(provincesList != null && provincesList.size() > 0){
            for(Map<String,Object> map : provincesList){
                provinces.add(map);
            }
        }
        if(sourcesList != null && sourcesList.size() > 0){
            for(Map<String,Object> map : sourcesList){
                sources.add(map);
            }
        }
        List<Map<String,Object>> cities = new ArrayList<>();
        Map<String,Object> cityMap = new HashMap<>();
        cityMap.put("value","all");
        cityMap.put("label","(全部)");
        cities.add(cityMap);
        Map<String,Object> res = new HashMap<>();
        res.put("countries",countries);
        res.put("provinces",provinces);
        res.put("sources",sources);
        res.put("cities",cities);
        return AjaxResult.success(res);
    }

    /**
     * 导出客户公共池列表
     */
    @Log(title = "客户公共池", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCrmKhggc sysCrmKhggc) {
        SysCrmKhggc param = new SysCrmKhggc();
        if(sysCrmKhggc.getKhProvince()!=null){
            if(!sysCrmKhggc.getKhProvince().equalsIgnoreCase("all")){
                param.setKhProvince(sysCrmKhggc.getKhProvince());
            }
        }
        if(sysCrmKhggc.getKhCity()!=null){
            if(!sysCrmKhggc.getKhCity().equalsIgnoreCase("all")){
                param.setKhCity(sysCrmKhggc.getKhCity());
            }
        }
        if(sysCrmKhggc.getKhFrom()!=null){
            if(!sysCrmKhggc.getKhFrom().equalsIgnoreCase("all")){
                param.setKhFrom(sysCrmKhggc.getKhFrom());
            }
        }
        if(sysCrmKhggc.getKhName()!=null&&!"".equals(sysCrmKhggc.getKhName())){
            param.setKhName(sysCrmKhggc.getKhName().replaceAll(" ",""));
        }
        if(sysCrmKhggc.getKhHandset()!=null&&!"".equals(sysCrmKhggc.getKhHandset())){
            param.setKhHandset(sysCrmKhggc.getKhHandset().replaceAll(" ",""));
        }
        List<SysCrmKhggc> list = sysCrmKhggcService.selectSysCrmKhggcList(param);
        ExcelUtil<SysCrmKhggc> util = new ExcelUtil<SysCrmKhggc>(SysCrmKhggc.class);
        util.exportExcel(response, list, "客户公共池数据");
    }

    /**
     * 获取客户公共池详细信息
     */
    @GetMapping(value = "/{khId}")
    public AjaxResult getInfo(@PathVariable("khId") String khId) {
        return success(sysCrmKhggcService.selectSysCrmKhggcByKhId(khId));
    }

    /**
     * 新增客户公共池
     */
    @Log(title = "客户公共池", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCrmKhggc sysCrmKhggc) {
        return toAjax(sysCrmKhggcService.insertSysCrmKhggc(sysCrmKhggc));
    }

    /**
     * 修改客户公共池
     */
    @Log(title = "客户公共池", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCrmKhggc sysCrmKhggc) {
        return toAjax(sysCrmKhggcService.updateSysCrmKhggc(sysCrmKhggc));
    }

    /**
     * 删除客户公共池
     */
    @Log(title = "客户公共池", businessType = BusinessType.DELETE)
	@DeleteMapping("/{khIds}")
    public AjaxResult remove(@PathVariable String[] khIds) {
        return toAjax(sysCrmKhggcService.deleteSysCrmKhggcByKhIds(khIds));
    }
}
